package com.shy.example.service.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.shy.example.service.consumer.service.DeptHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("dept")
@Slf4j
@DefaultProperties(defaultFallback = "default_FallbackMethod")
public class DeptController {

    @Resource
    private DeptHystrixService deptHystrixService;

    @GetMapping("hystrix/ok/{id}")
    public String deptInfo_Ok(@PathVariable("id") Integer id){
        String result = deptHystrixService.deptInfo_Ok(id);
        log.info("result: {}", result);
        return result;
    }

    @GetMapping("hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "deptInfo_TimeoutHandler")
    public String deptInfo_Timeout(@PathVariable("id") Integer id){
        String result =  deptHystrixService.deptInfo_Timeout(id);
        log.info("result: {}", result);
        return result;
    }

    @GetMapping("hystrix/timeout/default/{id}")
    @HystrixCommand
    public String deptInfo_Timeout_default(@PathVariable("id") Integer id){
        String result = deptHystrixService.deptInfo_Timeout(id);
        log.info("result: {}", result);
        return result;
    }

    public String deptInfo_TimeoutHandler(@PathVariable("id") Integer id){
        log.info("deptInfoTimeout 出错，服务已被降级！");
        return "本网站提醒您： 服务端系统繁忙，请稍后再试！";
    }

    public String default_FallbackMethod(){
        return "本网站提醒您： 运行出错获服务端系统繁忙，请稍后再试！";
    }
}
